package com.aice.chinamapview.component;

import com.aice.chinamapview.moudle.MycolorArea;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import java.math.BigDecimal;
import java.util.List;

/**
 * ColorView
 *
 * @author jjy
 * @since 2021-05-10
 */
public class ColorView extends Component implements Component.DrawTask {
    private static final int AVERAGES = 5;
    private static final int HARFVALUE = 2;
    private static final int HEIGHTAVERAGES = 3;
    private Paint colorPaint;
    private List<MycolorArea> list;

    /**
     * 构造函数
     *
     * @param context context对象
     */
    public ColorView(Context context) {
        super(context);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性值
     */
    public ColorView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        colorPaint = new Paint();
        colorPaint.setAntiAlias(true);
        colorPaint.setTextAlign(TextAlignment.CENTER);
        invalidate();
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性值
     * @param styleName 样式名
     */
    public ColorView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * 设置颜色值
     *
     * @param list 颜色数据
     */
    public void setList(List<MycolorArea> list) {
        this.list = list;
        invalidate();
    }

    @Override
    public void invalidate() {
        super.invalidate();
        this.addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (list == null || list.size() == 0) {
            return;
        } else {
            int widthAverage = getWidth() / list.size();
            for (int index = 0; index < list.size(); index++) {
                colorPaint.setColor(new Color(list.get(index).getColor()));
                float bottom = new BigDecimal(getHeight()).divide(new BigDecimal(HEIGHTAVERAGES), HARFVALUE)
                    .floatValue();
                float value = new BigDecimal(AVERAGES).divide(new BigDecimal(HARFVALUE), HARFVALUE).
                    multiply(new BigDecimal(getHeight()).divide(new BigDecimal(HEIGHTAVERAGES), HARFVALUE))
                    .floatValue();
                float va = new BigDecimal(widthAverage).divide(new BigDecimal(HARFVALUE), HARFVALUE).
                    add(new BigDecimal(new BigDecimal(index).
                        multiply(new BigDecimal(widthAverage)).floatValue())).floatValue();
                canvas.drawRect(index * widthAverage, 0f, (index + 1) * widthAverage,
                    bottom, colorPaint);
                colorPaint.setColor(Color.BLACK);
                colorPaint.setTextSize(getHeight() / HEIGHTAVERAGES);
                canvas.drawText(colorPaint, list.get(index).getText(), va, value);
            }
        }
    }
}
